home *** CD-ROM | disk | FTP | other *** search
/ SuperHack / SuperHack CD.bin / CODING / MISC / BM192A.ZIP / DOOM.ASM < prev    next >
Encoding:
Assembly Source File  |  1996-02-23  |  57.7 KB  |  2,116 lines

  1.  
  2.     nolist
  3.  
  4. ;*======================================================*
  5. ;*    DSP Core: latest update 23/02/96        *
  6. ;*======================================================*
  7. ;*    Perform all intensive & mathematical jobs    *
  8. ;*======================================================*
  9.  
  10. PBC            =    $FFE0
  11. HCR            =    $FFE8
  12. HSR            =    $FFE9
  13. HTX            =    $FFEB
  14.  
  15. ;-------------------------------------------------------*
  16.  
  17. runlink            =    0
  18. rundata            =    1
  19. runsize            =    2
  20.  
  21. ZMIN            =    12
  22.  
  23. ;-------------------------------------------------------*
  24.  
  25.     include        "i:\projects\bad_mood\68030\include\dspconst.s"
  26.  
  27. ;-------------------------------------------------------*
  28.     org        y:0
  29. ;-------------------------------------------------------*
  30.  
  31. map_data:
  32. map_xi:            ds    1
  33. map_x:            ds    1
  34. map_yi:            ds    1
  35. map_y:            ds    1
  36.  
  37. map_x1:            ds    1
  38. map_v:            ds    1
  39. map_xo:            ds    1
  40. map_uvs:        ds    1
  41. map_sinauvs:        ds    1
  42. map_cosauvs:        ds    1
  43.  
  44. ;-------------------------------------------------------*
  45.     org        x:0
  46. ;-------------------------------------------------------*
  47.  
  48. temp1:            ds    1
  49. temp2:            ds    1
  50.  
  51. ;-------------------------------------------------------*
  52.     org        l:2
  53. ;-------------------------------------------------------*
  54.  
  55. edge_increments:    
  56. y1_increment:        ds    1    ; 24:24
  57. y1_accumulator:        ds    1    ; 24:24
  58. y2_increment:        ds    1    ; 24:24
  59. y2_accumulator:        ds    1    ; 24:24
  60. t_increment:        ds    1    ; 24:24
  61. t_accumulator:        ds    1    ; 24:24
  62.  
  63. ;-------------------------------------------------------*
  64. ;    org        x:8
  65. ;-------------------------------------------------------*
  66.  
  67. column_thistop:        ds    1
  68. column_lasttop:        ds    1
  69.  
  70. ;-------------------------------------------------------*
  71.     org        l:10
  72. ;-------------------------------------------------------*
  73.  
  74. vertices:
  75.  
  76. vertex1:
  77. ctop_z:            ds    1
  78. cbot_z:            ds    1
  79.  
  80. vertex2:
  81. ftop_z:            ds    1
  82. fbot_z:            ds    1
  83.  
  84. vertex3:
  85. t:            ds    1
  86. ti:            ds    1
  87.  
  88. vertex4:
  89.             ds    1
  90.             ds    1
  91.  
  92. ;-------------------------------------------------------*
  93.     org        x:18
  94. ;-------------------------------------------------------*
  95.  
  96. ;-------------------------------------------------------*
  97. ;    X buffers                    *
  98. ;-------------------------------------------------------*
  99.  
  100. flatstack:        ds    256
  101.  
  102. miny:            ds    320+1
  103.  
  104. runs:
  105.  
  106. ;-------------------------------------------------------*
  107.     org        y:8
  108. ;-------------------------------------------------------*
  109.  
  110. column_thisbot:        ds    1
  111. column_lastbot:        ds    1
  112.  
  113. ;-------------------------------------------------------*
  114.     org        y:18
  115. ;-------------------------------------------------------*
  116.     
  117. ;-------------------------------------------------------*
  118. ;    Fast program registers                *
  119. ;-------------------------------------------------------*
  120.  
  121. x1_msk:            dc    $000FFF
  122. x2_msk:            dc    $FFF000
  123.  
  124. column_rout:        dc    perspected_column
  125. runsize_reg:        dc    runsize
  126. one:            dc    $000001
  127. two:            dc    $000002
  128. zmin:            dc    ZMIN
  129. pnorm:            dc    1<<((6+7+8+8)-24-1)
  130.  
  131. rshft19:
  132. lshft5:            dc    (1<<(5-1))
  133. rshft18:
  134. lshft6:            dc    (1<<(6-1))
  135. rshft16:
  136. lshft8:            dc    (1<<(8-1))
  137. rshft14:
  138. lshft10:        dc    (1<<(10-1))
  139. rshft13:
  140. lshft11:        dc    (1<<(11-1))
  141. rshft12:
  142. lshft12:        dc    (1<<(12-1))
  143. rshft9:
  144. lshft15:        dc    (1<<(15-1))
  145. rshft8:
  146. lshft16:        dc    (1<<(16-1))
  147. rshft7:
  148. lshft17:        dc    (1<<(17-1))
  149. rshft5:
  150. lshft19:        dc    (1<<(19-1))
  151.  
  152. HTX_ptr:        dc    HTX
  153. flatstack_ptr:        dc    flatstack
  154. lastslots_ptr:        dc    lastslots
  155. rundata_ptr:        dc    (runs+rundata)
  156. runlink_ptr:        dc    (runs+runlink)
  157. x1list_ptr:        dc    x1list
  158. x2list_ptr:        dc    x2list
  159. oslist_ptr:        dc    oslist
  160. tlist_ptr:        dc    tlist
  161. commands_ptr:        dc    commands
  162. octs_ptr:        dc    octs
  163.  
  164. index:            ds    1
  165. attributes:        ds    1
  166. columns:        ds    1
  167.  
  168. window_regs:
  169. width:            ds    1
  170. hwid:            ds    1
  171. height:            ds    1
  172. hhig:            ds    1
  173. r_height:        ds    1
  174. hs_width:        ds    1
  175. vs_width:        ds    1
  176.  
  177. ceiling_bounds:
  178. ctop:            ds    1
  179. cbot:            ds    1
  180.  
  181. floor_bounds:
  182. ftop:            ds    1
  183. fbot:            ds    1
  184.  
  185. noderegs:
  186. node_y2:        ds    1
  187. node_y1:        ds    1
  188. node_x1:        ds    1
  189. node_x2:        ds    1
  190.  
  191. px:            ds    1
  192. py:            ds    1
  193. pangle:            ds    1
  194. sina:            ds    1
  195. cosa:            ds    1
  196.  
  197. node_imin:        ds    1
  198. node_imax:        ds    1
  199.  
  200. map_tnorm:        ds    1
  201.  
  202. wall_u:            ds    1
  203. wall_umag:        dc    128
  204. wall_vmag:        dc    128
  205. wall_vsize:        dc    128
  206. wall_uoff:        ds    1
  207.  
  208. wall_sz:        ds    1
  209. wall_fsz:        ds    1
  210. wall_dz:        ds    1
  211. wall_fdz:        ds    1
  212.  
  213. luminance:        ds    1
  214. mlum2:            ds    1
  215. mlum3:            ds    1
  216. mlum_limit:        dc    64-1
  217. z:            ds    1
  218.  
  219. ci1:            ds    1
  220. ci2:            ds    1
  221.  
  222. wall_z1:        ds    1
  223. wall_z2:        ds    1
  224.  
  225. cy:            ds    1
  226. cj1:            ds    1
  227. cj2:            ds    1
  228. fy:            ds    1
  229. fj1:            ds    1
  230. fj2:            ds    1
  231.  
  232. ;-------------------------------------------------------*
  233. ;    Y buffers                    *
  234. ;-------------------------------------------------------*
  235.  
  236. maxy:            ds    320+1
  237.  
  238. ;-------------------------------------------------------*
  239. ;    DSP Process command list            *
  240. ;-------------------------------------------------------*
  241.  
  242. commands:        dc    AddMidWall
  243.             dc    AddUpperWall
  244.             dc    AddLowerWall
  245.             dc    ResizeWindow
  246.             dc    NewScene
  247.             dc    NewSsector
  248.             dc    GetSsector
  249.             dc    NodeInCone
  250.             dc    ScanOcclusion
  251.             dc    RotateMap
  252.             dc    LinearCalc
  253.             dc    PerspectCalc
  254.             dc    SetLuminance
  255.  
  256. octs:            dc    oct_2
  257.             dc    oct_3
  258.             dc    oct_4
  259.             dc    oct_5
  260.             dc    oct_6
  261.             dc    oct_7
  262.             dc    oct_0
  263.             dc    oct_1
  264.  
  265. linedefs:        dc    vertex1,vertex2,vertex3,vertex4,vertex1
  266.  
  267. ;-------------------------------------------------------*
  268. end_iy:
  269. ;-------------------------------------------------------*
  270.  
  271. ;-------------------------------------------------------*
  272.  
  273. dspread_s        macro
  274. _rd    jclr        #0,x:<<HSR,_rd
  275.     move        x:(r7),n7
  276.     move        n7,a
  277.     jclr        #15,n7,_sk
  278.     add        b,a
  279. _sk:    
  280.     endm
  281.  
  282. dspread_u        macro    acc
  283. _rd    jclr        #0,x:<<HSR,_rd
  284.     move        x:(r7),n7
  285.     move        n7,acc
  286.     endm
  287.  
  288. dspread            macro    var
  289. _rd    jclr        #0,x:<<HSR,_rd
  290.     movep        x:<<HTX,var
  291.     endm
  292.  
  293. dspwrite        macro    var
  294. _wr    jclr        #1,x:<<HSR,_wr
  295.     movep        var,x:<<HTX
  296.     endm
  297.  
  298. dspwaitread        macro
  299. _rd    jclr        #0,x:<<HSR,_rd
  300.     endm
  301.  
  302. dspwaitwrite        macro
  303. _wr    jclr        #1,x:<<HSR,_wr
  304.     endm
  305.  
  306. advance_column            macro
  307.     move            l:y1_increment,a
  308.     move            l:y1_accumulator,b
  309.     add        a,b    l:y2_increment,a
  310.     move            b,l:y1_accumulator
  311.     move            l:y2_accumulator,b
  312.     add        a,b    l:t_increment,a
  313.     move            b,l:y2_accumulator
  314.     move            l:t_accumulator,b
  315.     add        a,b    n0,a
  316.     move            y:one,x0
  317.     add        x0,a    b,l:t_accumulator
  318.     move        a,n0
  319.     move        a,n4
  320.     endm
  321.  
  322. load_column            macro
  323.     move            #0,x0
  324.     move            x:y1_accumulator,a
  325.     cmp        x0,a    y:(r4+n4),x1
  326.     tmi        x0,a
  327.     cmp        x1,a    x:(r0+n0),x0
  328.     tpl        x1,a
  329.     move        a,y0
  330.     move            x:y2_accumulator,a
  331.     cmp        x0,a    y:height,x1
  332.     tmi        x0,a
  333.     cmp        x1,a    (r7)+
  334.     tpl        x1,a
  335.     move        a,y1
  336.     endm
  337.  
  338. ;    thisminy    x:(r0+n0)
  339. ;    thismaxy    y:(r4+n4)
  340. ;    lastminy    x:(r0)
  341. ;    lastmaxy    y:(r4)
  342. ;    thistop        x:(r3)
  343. ;    thisbot        y:(r5)
  344. ;    lasttop        x:(r3+n3)
  345. ;    lastbot        y:(r5+n5)
  346.  
  347. ;-------------------------------------------------------*
  348.     org        p:0
  349. ;-------------------------------------------------------*
  350.     jmp        start
  351. ;-------------------------------------------------------*
  352.     org        p:64
  353. ;-------------------------------------------------------*
  354.  
  355. ;-------------------------------------------------------*
  356. end_flat:
  357. ;-------------------------------------------------------*
  358.     move        a,n6
  359.     move        b,n2    
  360.     add        a,b    y:(r1)+,x0
  361.     cmp        x0,a    y:(r1)-,x1
  362.     tpl        x0,a
  363.     cmp        x1,b    a,y:(r1)+
  364.     tmi        x1,b
  365.     move            b,y:(r1)
  366. ;-------------------------------------------------------*
  367.     move        y:flatstack_ptr,r6
  368.     move        y0,x:temp1        ; store old ytop
  369.     lua        (r6)+n6,r1
  370.     move        y:lastslots_ptr,r6
  371.     move        n0,x0
  372.     move        y:lshft12,x1
  373.     mpy        x0,x1,b        (r6)+n6
  374.     move        b0,b            ; b = (x2<<12)
  375.     move        y:x2_msk,y0        ; x0 = x2:x1 mergemask 
  376.     move        y:runsize_reg,x0
  377.     move        r7,n6            ; store old occlusion-list
  378.     move        y:rundata_ptr,r7    
  379.     move        y:index,a
  380.     do        n2,_vert
  381.     move                    y:(r6),n2    ; lastindex = lastslot(y)
  382.     and        y0,b    x:(r1)+,x1    a,y:(r6)+    ; fetch x1 : lastslot(y) = index
  383.     or        x1,b    a,n7                ; rundata = ((x2<<12)|x1)
  384.     move            a1,x:(r2+n2)            ; runs(lastslot(y)(next)) = index
  385.     add        x0,a    b1,x:(r7+n7)            ; runs(index(data)) = rundata : index+runsize
  386. _vert:    move        a,y:index
  387.     move        n6,r7            ; store old occlusion-list
  388.     move        x:temp1,y0        ; store old ytop
  389.     rts
  390.  
  391. ;-------------------------------------------------------*
  392. AddMidWall:
  393. ;-------------------------------------------------------*
  394.     jsr        init_addwall
  395. ;-------------------------------------------------------*
  396.     do        b,midwall_loop
  397. ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*
  398. ;    Advance edge accumulators            *
  399. ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*
  400.     load_column
  401. ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*
  402. ;    Generate floor buckets                *
  403. ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*
  404. ;    clip if [thisbot]=>[thismaxy]            *
  405. ;-------------------------------------------------------*
  406.     tfr        y1,b    y:(r4+n4),a        ; [thisbot] : [thismaxy]
  407.     cmp        a,b
  408.     jpl        _clip_lower
  409. ;-------------------------------------------------------*
  410. ;    draw if [lastbot]>[thisbot]            *
  411. ;-------------------------------------------------------*
  412.     tfr        y1,a    y:(r5+n5),b        ; [thisbot] : [lastbot]
  413.     sub        a,b
  414.     jsgt        start_floor            ; y1=[thisbot] : y2=[lastbot]
  415. ;-------------------------------------------------------*
  416. ;    draw if [thismaxy]>[lastmaxy]            *
  417. ;-------------------------------------------------------*
  418.     move        y:(r4),a            ; [lastmaxy]
  419.     move        y:(r4+n4),b            ; [thismaxy]
  420.     sub        a,b
  421.     jsgt        start_floor            ; y1=[lastmaxy] : y2=[thismaxy]
  422. ;-------------------------------------------------------*
  423. ;    draw if [thisbot]>[lastbot]            *
  424. ;-------------------------------------------------------*
  425.     tfr        y1,b    y:(r5+n5),a        ; [thisbot] : [lastbot]
  426.     sub        a,b    #floor_bounds,r1
  427.     jsgt        end_flat            ; y1=[thisbot] : y2=[lastbot]
  428. ;-------------------------------------------------------*
  429. ;    draw if [lastmaxy]>[thismaxy]            *
  430. ;-------------------------------------------------------*
  431.     move        y:(r4+n4),a            ; [thismaxy]
  432.     move        y:(r4),b            ; [lastmaxy]
  433.     sub        a,b    #floor_bounds,r1
  434.     jsgt        end_flat            ; y1=[thismaxy] : y2=[lastmaxy]
  435. ;-------------------------------------------------------*
  436.     jmp        _floor_done
  437. ;-------------------------------------------------------*
  438. _clip_lower:
  439. ;-------------------------------------------------------*
  440. ;    draw if [lastmaxy]>[lastbot]            *
  441. ;-------------------------------------------------------*
  442.     move        y:(r5+n5),a            ; [lastbot]
  443.     move        y:(r4),b            ; [lastmaxy]
  444.     sub        a,b    #floor_bounds,r1
  445.     jsgt        end_flat            ; y1=[lastbot] : y2=[lastmaxy]
  446. ;-------------------------------------------------------*
  447. _floor_done:    
  448. ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*
  449. ;    Generate ceiling buckets            *
  450. ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*
  451. ;    clip if [thistop]<=[thisminy]            *
  452. ;-------------------------------------------------------*
  453.     tfr        y0,b    x:(r0+n0),a        ; [thistop] : [thisminy]
  454.     cmp        a,b
  455.     jle        _clip_upper    
  456. ;-------------------------------------------------------*
  457. ;    draw if [thistop]>[lasttop]            *
  458. ;-------------------------------------------------------*
  459.     tfr        y0,b    x:(r3+n3),a        ; [thistop] : [lasttop]
  460.     sub        a,b
  461.     jsgt        start_ceiling            ; y1=[lasttop] : y2=[thistop]
  462. ;-------------------------------------------------------*
  463. ;    draw if [lastminy]>[thisminy]            *
  464. ;-------------------------------------------------------*
  465.     move        x:(r0+n0),a            ; [thisminy]
  466.     move        x:(r0),b            ; [lastminy]
  467.     sub        a,b
  468.     jsgt        start_ceiling            ; y1=[thisminy] : y2=[lastminy]
  469. ;-------------------------------------------------------*
  470. ;    draw if [lasttop]>[thistop]            *
  471. ;-------------------------------------------------------*
  472.     tfr        y0,a    x:(r3+n3),b        ; [thistop] : [lasttop]
  473.     sub        a,b    #ceiling_bounds,r1
  474.     jsgt        end_flat            ; y1=[thistop] : y2=[lasttop]
  475. ;-------------------------------------------------------*
  476. ;    draw if [thisminy]>[lastminy]            *
  477. ;-------------------------------------------------------*
  478.     move        x:(r0),a            ; [lastminy]
  479.     move        x:(r0+n0),b            ; [thisminy]
  480.     sub        a,b    #ceiling_bounds,r1
  481.     jsgt        end_flat            ; y1=[lastminy] : y2=[thisminy]
  482. ;-------------------------------------------------------*
  483.     jmp        _ceiling_done
  484. ;-------------------------------------------------------*
  485. _clip_upper:
  486. ;-------------------------------------------------------*
  487.     move        x:(r0),a            ; [lastminy]
  488.     move        x:(r3+n3),b            ; [lasttop]
  489.     sub        a,b    #ceiling_bounds,r1
  490.     jsgt        end_flat            ; y1=[lastminy] : y2=[lasttop]
  491. ;-------------------------------------------------------*
  492. _ceiling_done:    
  493. ;-------------------------------------------------------*
  494. ;    Rotate column history registers            *
  495. ;-------------------------------------------------------*
  496. ;    lasttop = thistop / lastbot = thisbot        *
  497. ;-------------------------------------------------------*
  498.     move        y0,x:(r3+n3)
  499.     move        y1,y:(r5+n5)
  500. ;-------------------------------------------------------*
  501. ;    lastmin = thismin / lastmax = thismax        *
  502. ;-------------------------------------------------------*
  503.     move        x:(r0+n0),a
  504.     move        y:(r4+n4),b
  505. ;-------------------------------------------------------*
  506. ;    Check - column is already sealed off        *
  507. ;-------------------------------------------------------*
  508.     cmp        a,b    a,x:(r0)    b,y:(r4)
  509.     jle        _skip
  510. ;-------------------------------------------------------*
  511. ;    Check - column below [maxy] - blocked by roof    *
  512. ;-------------------------------------------------------*
  513.     cmp        y0,b
  514.     jle        _update_window
  515. ;-------------------------------------------------------*
  516. ;    Check - column above [miny] - blocked by floor    *
  517. ;-------------------------------------------------------*
  518.     cmp        y1,a
  519.     jpl        _update_window
  520. ;-------------------------------------------------------*
  521. ;    Clip - top of column cropped by [miny]        *
  522. ;-------------------------------------------------------*
  523.     cmp        y0,a
  524.     jle        _ncl1
  525.     move        a,y0
  526. ;-------------------------------------------------------*
  527. ;    Clip - bottom of column cropped by [maxy]    *
  528. ;-------------------------------------------------------*
  529. _ncl1:    cmp        y1,b 
  530.     jpl        _ncl2
  531.     move        b,y1
  532. ;-------------------------------------------------------*
  533. ;    Check - column cropped to zero length        *
  534. ;-------------------------------------------------------*
  535. _ncl2:    tfr        y1,b    y:column_rout,r1
  536.     cmp        y0,b    n0,x0            ; [thistop]<>[thisbot]
  537.     jsgt        (r1)
  538. ;-------------------------------------------------------*
  539. ;    Update vertical clipping bar            *
  540. ;-------------------------------------------------------*
  541. _update_window:
  542. ;-------------------------------------------------------*
  543.     tfr        y0,a    y1,b
  544.     jset        #0,y:attributes,_trans
  545.     clr        b    y:height,a
  546. _trans:    move        a,x:(r0+n0)
  547.     move        b,y:(r4+n4)
  548.     cmp        a,b    #1,a1
  549.     jgt        _skip
  550.     move            y:columns,b
  551.     sub        a,b    b0,y:(r7)
  552.     move            b,y:columns        
  553. ;-------------------------------------------------------*
  554. ;    Advance to next column                *
  555. ;-------------------------------------------------------*
  556. _skip:    advance_column
  557. ;-------------------------------------------------------*
  558. midwall_loop:
  559. ;-------------------------------------------------------*
  560. ;    Fill gaps left in floor & ceiling by edges    *
  561. ;-------------------------------------------------------*
  562.     move        y:(r5+n5),a            ; [lastbot]
  563.     move        y:(r4),b            ; [lastmaxy]
  564.     sub        a,b    #floor_bounds,r1
  565.     jsgt        end_flat
  566.     move        x:(r0),a            ; [lastminy]
  567.     move        x:(r3+n3),b            ; [lasttop]
  568.     sub        a,b    #ceiling_bounds,r1
  569.     jsgt        end_flat
  570. ;-------------------------------------------------------*
  571.     jmp        end_addwall
  572.  
  573. ;-------------------------------------------------------*
  574. AddUpperWall:
  575. ;-------------------------------------------------------*
  576.     jsr        init_addwall
  577. ;-------------------------------------------------------*
  578.     do        b,upperwall_loop
  579. ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*
  580. ;    Advance edge accumulators            *
  581. ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*
  582.     load_column
  583. ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*
  584. ;    Generate ceiling buckets            *
  585. ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*
  586. ;    clip if [thistop]<=[thisminy]            *
  587. ;-------------------------------------------------------*
  588.     tfr        y0,b    x:(r0+n0),a        ; [thistop] : [thisminy]
  589.     cmp        a,b
  590.     jle        _clip_upper    
  591. ;-------------------------------------------------------*
  592. ;    draw if [thistop]>[lasttop]            *
  593. ;-------------------------------------------------------*
  594.     tfr        y0,b    x:(r3+n3),a        ; [thistop] : [lasttop]
  595.     sub        a,b
  596.     jsgt        start_ceiling            ; y1=[lasttop] : y2=[thistop]
  597. ;-------------------------------------------------------*
  598. ;    draw if [lastminy]>[thisminy]            *
  599. ;-------------------------------------------------------*
  600.     move        x:(r0+n0),a            ; [thisminy]
  601.     move        x:(r0),b            ; [lastminy]
  602.     sub        a,b
  603.     jsgt        start_ceiling            ; y1=[thisminy] : y2=[lastminy]
  604. ;-------------------------------------------------------*
  605. ;    draw if [lasttop]>[thistop]            *
  606. ;-------------------------------------------------------*
  607.     tfr        y0,a    x:(r3+n3),b        ; [thistop] : [lasttop]
  608.     sub        a,b    #ceiling_bounds,r1
  609.     jsgt        end_flat            ; y1=[thistop] : y2=[lasttop]
  610. ;-------------------------------------------------------*
  611. ;    draw if [thisminy]>[lastminy]            *
  612. ;-------------------------------------------------------*
  613.     move        x:(r0),a            ; [lastminy]
  614.     move        x:(r0+n0),b            ; [thisminy]
  615.     sub        a,b    #ceiling_bounds,r1
  616.     jsgt        end_flat            ; y1=[lastminy] : y2=[thisminy]
  617. ;-------------------------------------------------------*
  618.     jmp        _ceiling_done
  619. ;-------------------------------------------------------*
  620. _clip_upper:
  621. ;-------------------------------------------------------*
  622.     move        x:(r0),a            ; [lastminy]
  623.     move        x:(r3+n3),b            ; [lasttop]
  624.     sub        a,b    #ceiling_bounds,r1
  625.     jsgt        end_flat            ; y1=[lastminy] : y2=[lasttop]
  626. ;-------------------------------------------------------*
  627. _ceiling_done:    
  628. ;-------------------------------------------------------*
  629. ;    Rotate column history registers            *
  630. ;-------------------------------------------------------*
  631. ;    lasttop = thistop / lastbot = thisbot        *
  632. ;-------------------------------------------------------*
  633.     move        y0,x:(r3+n3)
  634.     move        y1,y:(r5+n5)
  635. ;-------------------------------------------------------*
  636. ;    lastmin = thismin / lastmax = thismax        *
  637. ;-------------------------------------------------------*
  638.     move        x:(r0+n0),a
  639.     move        y:(r4+n4),b
  640. ;-------------------------------------------------------*
  641. ;    Check - column is already sealed off        *
  642. ;-------------------------------------------------------*
  643.     cmp        a,b    a,x:(r0)    b,y:(r4)
  644.     jle        _skip
  645. ;-------------------------------------------------------*
  646. ;    Check - column below [maxy] - blocked by roof    *
  647. ;-------------------------------------------------------*
  648.     cmp        y1,a
  649.     jpl        _skip
  650. ;-------------------------------------------------------*
  651. ;    Check - column above [miny] - blocked by floor    *
  652. ;-------------------------------------------------------*
  653.     cmp        y0,b
  654.     jle        _update_window
  655. ;-------------------------------------------------------*
  656. ;    Clip - top of column cropped by [miny]        *
  657. ;-------------------------------------------------------*
  658.     cmp        y0,a
  659.     jle        _ncl1
  660.     move        a,y0
  661. ;-------------------------------------------------------*
  662. ;    Clip - bottom of column cropped by [maxy]    *
  663. ;-------------------------------------------------------*
  664. _ncl1:    cmp        y1,b 
  665.     jpl        _ncl2
  666.     move        b,y1
  667. ;-------------------------------------------------------*
  668. ;    Check - column cropped to zero length        *
  669. ;-------------------------------------------------------*
  670. _ncl2:    tfr        y1,b    y:column_rout,r1
  671.     cmp        y0,b    n0,x0            ; [thistop]<>[thisbot]
  672.     jsgt        (r1)
  673. ;-------------------------------------------------------*
  674. ;    Update vertical clipping bar            *
  675. ;-------------------------------------------------------*
  676. _update_window:
  677. ;-------------------------------------------------------*
  678.     move            y:(r4+n4),b
  679.     cmp        y1,b    #1,a1
  680.     jgt        _upd
  681.     move            y:columns,b
  682.     sub        a,b    b0,y:(r7)
  683.     move            b,y:columns
  684. _cont:    move            b0,y:(r4+n4)
  685.     move            y:height,y1
  686. _upd:    move            y1,x:(r0+n0)
  687. ;-------------------------------------------------------*
  688. ;    Advance to next column                *
  689. ;-------------------------------------------------------*
  690. _skip:    advance_column
  691. ;-------------------------------------------------------*
  692. upperwall_loop:
  693. ;-------------------------------------------------------*
  694. ;    Fill gaps left in floor & ceiling by edges    *
  695. ;-------------------------------------------------------*
  696.     move        x:(r0),a
  697.     move        x:(r3+n3),b
  698.     sub        a,b    #ceiling_bounds,r1
  699.     jsgt        end_flat
  700. ;-------------------------------------------------------*
  701.     jmp        end_addwall
  702.  
  703. ;-------------------------------------------------------*
  704. AddLowerWall:
  705. ;-------------------------------------------------------*
  706.     jsr        init_addwall
  707. ;-------------------------------------------------------*
  708.     do        b,lowerwall_loop
  709. ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*
  710. ;    Advance edge accumulators            *
  711. ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*
  712.     load_column
  713. ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*
  714. ;    Generate floor buckets                *
  715. ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*
  716. ;    clip if [thisbot]=>[thismaxy]            *
  717. ;-------------------------------------------------------*
  718.     tfr        y1,b    y:(r4+n4),a        ; [thisbot] : [thismaxy]
  719.     cmp        a,b
  720.     jpl        _clip_lower
  721. ;-------------------------------------------------------*
  722. ;    draw if [lastbot]>[thisbot]            *
  723. ;-------------------------------------------------------*
  724.     tfr        y1,a    y:(r5+n5),b        ; [thisbot] : [lastbot]
  725.     sub        a,b
  726.     jsgt        start_floor            ; y1=[thisbot] : y2=[lastbot]
  727. ;-------------------------------------------------------*
  728. ;    draw if [thismaxy]>[lastmaxy]            *
  729. ;-------------------------------------------------------*
  730.     move        y:(r4),a            ; [lastmaxy]
  731.     move        y:(r4+n4),b            ; [thismaxy]
  732.     sub        a,b
  733.     jsgt        start_floor            ; y1=[lastmaxy] : y2=[thismaxy]
  734. ;-------------------------------------------------------*
  735. ;    draw if [thisbot]>[lastbot]            *
  736. ;-------------------------------------------------------*
  737.     tfr        y1,b    y:(r5+n5),a        ; [thisbot] : [lastbot]
  738.     sub        a,b    #floor_bounds,r1
  739.     jsgt        end_flat            ; y1=[thisbot] : y2=[lastbot]
  740. ;-------------------------------------------------------*
  741. ;    draw if [lastmaxy]>[thismaxy]            *
  742. ;-------------------------------------------------------*
  743.     move        y:(r4+n4),a            ; [thismaxy]
  744.     move        y:(r4),b            ; [lastmaxy]
  745.     sub        a,b    #floor_bounds,r1
  746.     jsgt        end_flat            ; y1=[thismaxy] : y2=[lastmaxy]
  747. ;-------------------------------------------------------*
  748.     jmp        _floor_done
  749. ;-------------------------------------------------------*
  750. _clip_lower:
  751. ;-------------------------------------------------------*
  752. ;    draw if [lastmaxy]>[lastbot]            *
  753. ;-------------------------------------------------------*
  754.     move        y:(r5+n5),a            ; [lastbot]
  755.     move        y:(r4),b            ; [lastmaxy]
  756.     sub        a,b    #floor_bounds,r1
  757.     jsgt        end_flat            ; y1=[lastbot] : y2=[lastmaxy]
  758. ;-------------------------------------------------------*
  759. _floor_done:    
  760. ;-------------------------------------------------------*
  761. ;    Rotate column history registers            *
  762. ;-------------------------------------------------------*
  763. ;    lasttop = thistop / lastbot = thisbot        *
  764. ;-------------------------------------------------------*
  765.     move        y0,x:(r3+n3)
  766.     move        y1,y:(r5+n5)
  767. ;-------------------------------------------------------*
  768. ;    lastmin = thismin / lastmax = thismax        *
  769. ;-------------------------------------------------------*
  770.     move        x:(r0+n0),a
  771.     move        y:(r4+n4),b
  772. ;-------------------------------------------------------*
  773. ;    Check - column is already sealed off        *
  774. ;-------------------------------------------------------*
  775.     cmp        a,b    a,x:(r0)    b,y:(r4)
  776.     jle        _skip
  777. ;-------------------------------------------------------*
  778. ;    Check - column below [maxy] - blocked by roof    *
  779. ;-------------------------------------------------------*
  780.     cmp        y0,b
  781.     jle        _skip
  782. ;-------------------------------------------------------*
  783. ;    Check - column above [miny] - blocked by floor    *
  784. ;-------------------------------------------------------*
  785.     cmp        y1,a
  786.     jpl        _update_window
  787. ;-------------------------------------------------------*
  788. ;    Clip - top of column cropped by [miny]        *
  789. ;-------------------------------------------------------*
  790.     cmp        y0,a
  791.     jle        _ncl1
  792.     move        a,y0
  793. ;-------------------------------------------------------*
  794. ;    Clip - bottom of column cropped by [maxy]    *
  795. ;-------------------------------------------------------*
  796. _ncl1:    cmp        y1,b 
  797.     jpl        _ncl2
  798.     move        b,y1
  799. ;-------------------------------------------------------*
  800. ;    Check - column cropped to zero length        *
  801. ;-------------------------------------------------------*
  802. _ncl2:    tfr        y1,b    y:column_rout,r1
  803.     cmp        y0,b    n0,x0            ; [thistop]<>[thisbot]
  804.     jsgt        (r1)
  805. ;-------------------------------------------------------*
  806. ;    Update vertical clipping bar            *
  807. ;-------------------------------------------------------*
  808. _update_window:
  809. ;-------------------------------------------------------*
  810.     move            x:(r0+n0),b
  811.     cmp        y0,b    #1,a1
  812.     jlt        _upd
  813.     move            y:columns,b
  814.     sub        a,b    b0,y:(r7)
  815.     move            b,y:columns
  816. _cont:    move            y:height,y1
  817.     move            y1,x:(r0+n0)
  818.     move            b0,y0    
  819. _upd:    move            y0,y:(r4+n4)
  820. ;-------------------------------------------------------*
  821. ;    Advance to next column                *
  822. ;-------------------------------------------------------*
  823. _skip:    advance_column
  824. ;-------------------------------------------------------*
  825. lowerwall_loop:
  826. ;-------------------------------------------------------*
  827. ;    Fill gaps left in floor & ceiling by edges    *
  828. ;-------------------------------------------------------*
  829.     move        y:(r5+n5),a
  830.     move        y:(r4),b
  831.     sub        a,b    #floor_bounds,r1
  832.     jsgt        end_flat
  833. ;-------------------------------------------------------*
  834.     jmp        end_addwall
  835.  
  836. ;-------------------------------------------------------*
  837. perspected_column:
  838. ;-------------------------------------------------------*
  839.     move                y:HTX_ptr,r1
  840. ;-------------------------------------------------------*
  841. ;    Write [i]                     *
  842. ;-------------------------------------------------------*
  843.     move                y:one,x0
  844.     move        n0,a
  845.     sub        x0,a        y:lshft8,x0
  846.     dspwaitwrite
  847.     mpy        x0,y0,a        a,x:(r1)
  848. ;-------------------------------------------------------*
  849. ;    Write [y1:y2] / calculate [u] factor        *
  850. ;-------------------------------------------------------*
  851.     move        a0,b
  852.     or        y1,b        x:t_accumulator,x0
  853.     move                y:lshft10,a
  854.     and        #$FE,ccr
  855.     rep        #24
  856.     div        x0,a
  857.     move        a0,a
  858.     move                y:wall_fsz,x1
  859.     sub        x1,a        y:wall_fdz,x1
  860.     abs        a        y:wall_dz,x0
  861.     dspwaitwrite
  862.     move                b,x:(r1)
  863. ;-------------------------------------------------------*
  864. ;    Calculate [u] index                *
  865. ;-------------------------------------------------------*
  866.     and        #$FE,ccr
  867.     rep        #24
  868.     div        x1,a
  869.     move        a0,x1
  870. ;-------------------------------------------------------*
  871. ;    Calculate [lum]                    *
  872. ;-------------------------------------------------------*
  873.     mpy        x0,x1,b        y:wall_z2,x0
  874.     sub        x0,b        y:lshft19,x1
  875.     add        x1,b        y:lshft5,x1
  876.     move        b1,x0
  877.     mpy        x0,x1,b
  878.     move        b0,x0
  879.     mpy        x0,x0,b
  880.     move        b1,x0
  881.     mpy        x0,x0,b        y:rshft5,x1
  882.     move        b1,x0
  883.     mpy        x0,x1,b        y:mlum2,x1
  884.     move        b1,x0
  885.     mpy        x0,x1,b        y:mlum3,x0
  886.     add        x0,b        #0,x1
  887.     tmi        x1,b
  888.     move                y:mlum_limit,x1
  889.     cmp        x1,b        y:lshft6,x0        
  890.     tpl        x1,b
  891.     move        b1,x1
  892.     mpy        x0,x1,b        a0,x1
  893. ;-------------------------------------------------------*
  894.     dspwaitwrite
  895.     move                b0,x:(r1)    
  896. ;-------------------------------------------------------*
  897.     move                y:wall_umag,x0
  898.     mpy        x0,x1,a        y:wall_u,x0
  899.     add        x0,a        y:rshft8,x0
  900.     move        a,x1
  901.     mpy        x0,x1,a        y:wall_uoff,x0
  902.     add        x0,a        #>(128-1),x0
  903.     and        x0,a        y:wall_vsize,x1
  904.     move        a,x0
  905.     mpy        x0,x1,a
  906.     asr        a        l:y2_accumulator,b
  907. ;-------------------------------------------------------*
  908. ;    Write [u]                    *
  909. ;-------------------------------------------------------*
  910.     dspwaitwrite
  911.     move                a0,x:(r1)
  912. ;-------------------------------------------------------*
  913. ;    Calculate [dv]                    *
  914. ;-------------------------------------------------------*
  915.     move                l:y1_accumulator,a
  916.     sub        a,b        y:wall_vmag,a
  917.     rep        #8
  918.     asl        b
  919.     asl        a        b,x1
  920.     and        #$FE,ccr
  921.     rep        #24
  922.     div        x1,a
  923.     dspwaitwrite
  924.     move                a0,x:(r1)
  925. ;-------------------------------------------------------*
  926. ;    Calculate [v] index                *
  927. ;-------------------------------------------------------*
  928.     move                l:y1_accumulator,b
  929.     sub        y0,b        a0,x1
  930.     neg        b        y:one,x0
  931.     add        x0,b
  932.     rep        #8
  933.     asl        b
  934.     clr        b        b,x0
  935.     mpy        x0,x1,a
  936.     rep        #8-1
  937.     asl        a
  938.     dspwaitwrite
  939.     clr        a        a,x:(r1)
  940.     rts
  941.  
  942. ;-------------------------------------------------------*
  943. linear_column:
  944. ;-------------------------------------------------------*
  945.     move                y:HTX_ptr,r1
  946. ;-------------------------------------------------------*
  947. ;    Write [i]                     *
  948. ;-------------------------------------------------------*
  949.     move                y:one,x0
  950.     move        n0,a
  951.     sub        x0,a        y:lshft8,x0
  952.     dspwaitwrite
  953.     mpy        x0,y0,a        a,x:(r1)
  954. ;-------------------------------------------------------*
  955. ;    Write [y1:y2] / calculate [u] factor        *
  956. ;-------------------------------------------------------*
  957.     move        a0,b
  958.     or        y1,b        x:t_accumulator,a
  959.     dspwaitwrite
  960.     move                b,x:(r1)
  961.     move                y:wall_sz,x1
  962.     sub        x1,a        y:wall_dz,x1    ; ez-sz
  963.     abs        a        y:wall_z1,b
  964. ;-------------------------------------------------------*
  965. ;    Calculate [u] index                *
  966. ;-------------------------------------------------------*
  967.     and        #$FE,ccr
  968.     rep        #24
  969.     div        x1,a
  970.     move                y:lshft19,x1
  971.     add        x1,b        y:lshft5,x1
  972.     move        b1,x0
  973.     mpy        x0,x1,b
  974.     move        b0,x0
  975.     mpy        x0,x0,b
  976.     move        b1,x0
  977.     mpy        x0,x0,b        y:rshft5,x1
  978.     move        b1,x0
  979.     mpy        x0,x1,b        y:mlum2,x1
  980.     move        b1,x0
  981.     mpy        x0,x1,b        y:mlum3,x0
  982.     add        x0,b        #0,x1
  983.     tmi        x1,b
  984.     move                y:mlum_limit,x1
  985.     cmp        x1,b        y:lshft6,x0        
  986.     tpl        x1,b
  987.     move        b1,x1
  988.     mpy        x0,x1,b        a0,x1
  989. ;-------------------------------------------------------*
  990.     dspwaitwrite
  991.     move                b0,x:(r1)    
  992. ;-------------------------------------------------------*
  993.     move                y:wall_umag,x0
  994.     mpy        x0,x1,a        y:wall_u,x0
  995.     add        x0,a        y:rshft8,x0
  996.     move        a,x1
  997.     mpy        x0,x1,a        y:wall_uoff,x0
  998.     add        x0,a        #>(128-1),x0
  999.     and        x0,a        y:wall_vsize,x1
  1000.     move        a,x0
  1001.     mpy        x0,x1,a
  1002.     asr        a        l:y2_accumulator,b
  1003. ;-------------------------------------------------------*
  1004. ;    Write [u]                    *
  1005. ;-------------------------------------------------------*
  1006.     dspwaitwrite
  1007.     move                a0,x:(r1)
  1008. ;-------------------------------------------------------*
  1009. ;    Calculate [dv]                    *
  1010. ;-------------------------------------------------------*
  1011.     move                l:y1_accumulator,a
  1012.     sub        a,b        y:wall_vmag,a
  1013.     rep        #8
  1014.     asl        b
  1015.     asl        a        b,x1
  1016.     and        #$FE,ccr
  1017.     rep        #24
  1018.     div        x1,a
  1019.     dspwaitwrite
  1020.     move                a0,x:(r1)
  1021. ;-------------------------------------------------------*
  1022. ;    Calculate [v] index                *
  1023. ;-------------------------------------------------------*
  1024.     move                l:y1_accumulator,b
  1025.     sub        y0,b        a0,x1
  1026.     neg        b        y:one,x0
  1027.     add        x0,b
  1028.     rep        #8
  1029.     asl        b
  1030.     clr        b        b,x0
  1031.     mpy        x0,x1,a
  1032.     rep        #8-1
  1033.     asl        a
  1034.     dspwaitwrite
  1035.     clr        a        a,x:(r1)
  1036.     rts
  1037.  
  1038. ;-------------------------------------------------------*
  1039. start_ceiling:
  1040. ;-------------------------------------------------------*
  1041. start_floor:
  1042. ;-------------------------------------------------------*
  1043.     move        y:flatstack_ptr,r1
  1044.     move        a,n1
  1045.     add        a,b    b1,n2
  1046.     lua        (r1)+n1,r1
  1047.     rep        n2
  1048.     move        n0,x:(r1)+    
  1049.     rts
  1050.     
  1051. ;-------------------------------------------------------*
  1052. end_addwall:
  1053. ;-------------------------------------------------------*
  1054.     move        #-1,a1
  1055.     dspwrite    a1
  1056.     dspwrite    y:columns
  1057.     jmp        command_base
  1058.  
  1059. ;-------------------------------------------------------*
  1060. read_real:
  1061. ;-------------------------------------------------------*
  1062. _rd1    jclr        #0,x:<<HSR,_rd1
  1063.     move        x:(r7),n7
  1064.     move        n7,x1
  1065.     mpy        x1,x0,a
  1066.     move        a0,y:(r6)
  1067. _rd2    jclr        #0,x:<<HSR,_rd2
  1068.     move        x:(r7),n7
  1069.     move        n7,a
  1070.     jclr        #15,n7,_sk
  1071.     add        b,a
  1072. _sk:    move        a1,x:(r6)
  1073.     rts
  1074.  
  1075. ;-------------------------------------------------------*
  1076. init_addwall:
  1077. ;-------------------------------------------------------*
  1078. ;    Get wall details                *    
  1079. ;-------------------------------------------------------*
  1080.     move        y:HTX_ptr,r7
  1081.     move        #>1<<(8-1),x0
  1082.     move        #<$FF,b
  1083. ;-------------------------------------------------------*
  1084. ;    Read y-increments & accumulators        *
  1085. ;-------------------------------------------------------*
  1086.     move        #y1_accumulator,r6
  1087.     jsr        read_real
  1088.     move        #y1_increment,r6
  1089.     jsr        read_real
  1090.     move        #y2_accumulator,r6
  1091.     jsr        read_real
  1092.     move        #y2_increment,r6
  1093.     jsr        read_real
  1094. ;-------------------------------------------------------*
  1095.     move        #temp1,r6
  1096. ;-------------------------------------------------------*
  1097. ;    Read z-increments & accumulators        *
  1098. ;-------------------------------------------------------*
  1099.     jsr        read_real
  1100.     move        l:(r6),a
  1101.     rep        #8
  1102.     asl        a
  1103.     move        a,l:t_accumulator
  1104.     jsr        read_real
  1105.     move        l:(r6),a
  1106.     rep        #8
  1107.     asl        a
  1108.     move        a,l:t_increment
  1109. ;-------------------------------------------------------*
  1110. ;    Read z-bounds                    *
  1111. ;-------------------------------------------------------*
  1112.     jsr        read_real
  1113.     move        l:(r6),a
  1114.     rep        #8
  1115.     asl        a
  1116.     move        a,y:wall_z1
  1117.     jsr         read_real
  1118.     move        l:(r6),a
  1119.     rep        #8
  1120.     asl        a
  1121.     move        a,y:wall_z2
  1122. ;-------------------------------------------------------*
  1123. ;    Read inital texture index & width        *
  1124. ;-------------------------------------------------------*
  1125.     jsr        read_real
  1126.     move        l:(r6),a
  1127.     rep        #8
  1128.     asl        a
  1129.     move        a,y:wall_u
  1130.     jsr        read_real
  1131.     move        l:(r6),a
  1132.     rep        #8
  1133.     asl        a
  1134.     move        a,y:wall_umag
  1135. ;-------------------------------------------------------*
  1136.     dspread        y:wall_vmag
  1137.     dspread        y:wall_vsize
  1138.     dspread        y:wall_uoff
  1139. ;-------------------------------------------------------*
  1140.     dspread        y:ci1
  1141. ;-------------------------------------------------------*
  1142.     move        y:wall_z1,a
  1143.     move        a,y:wall_sz
  1144.     abs        a        a,y:0
  1145.     move        a,x0
  1146.     move        y:lshft10,a
  1147.     and        #$FE,ccr
  1148.     rep        #24
  1149.     div        x0,a
  1150.     move        a0,a
  1151.     jclr        #23,y:0,_p1
  1152.     neg        a
  1153. _p1:    move        a,y:wall_fsz
  1154. ;-------------------------------------------------------*
  1155.     dspread        y:ci2
  1156. ;-------------------------------------------------------*
  1157.     move        y:wall_z2,a
  1158.     move        y:wall_z1,b
  1159.     sub        b,a
  1160.     move        a,y:wall_dz
  1161.     move        y:wall_z1,a
  1162.     abs        a        a,y:0
  1163.     move        a,x0
  1164.     move        y:lshft10,a
  1165.     and        #$FE,ccr
  1166.     rep        #24
  1167.     div        x0,a
  1168.     move        a0,b
  1169.     jclr        #23,y:0,_p2
  1170.     neg        b
  1171. _p2:    move        y:wall_z2,a
  1172.     abs        a        a,y:0
  1173.     move        a,x0
  1174.     move        y:lshft10,a
  1175.     and        #$FE,ccr
  1176.     rep        #24
  1177.     div        x0,a
  1178.     move        a0,a
  1179.     jclr        #23,y:0,_p3
  1180.     neg        a
  1181. _p3:    sub        b,a
  1182.     move        a,y:wall_fdz
  1183. ;-------------------------------------------------------*
  1184. ;    Read x1 & x2                    *
  1185. ;-------------------------------------------------------*
  1186.     dspread        y:attributes
  1187.     dspread        y:columns
  1188. ;-------------------------------------------------------*
  1189.     move        #column_thistop,r3
  1190.     move        #column_thisbot,r5
  1191.     move        #1,n3
  1192.     move        #1,n5
  1193. ;-------------------------------------------------------*
  1194. ;    Init miny maxy & occlusion lists        *
  1195. ;-------------------------------------------------------*
  1196.     clr        b    #(occlusion_list-1),r1
  1197.     move        y:ci1,a
  1198.     move        a1,n1
  1199.     move        #miny,r0
  1200.     lua        (r1)+n1,r7
  1201.     move        #maxy,r4
  1202.     move        #1,b1
  1203.     add        b,a    y:runlink_ptr,r2
  1204.     move        a,n0
  1205.     move        a,n4
  1206. ;-------------------------------------------------------*
  1207. ;    Prefetch loop registers                *
  1208. ;-------------------------------------------------------*
  1209.     move        x:(r0+n0),a
  1210.     move                y:(r4+n4),b
  1211.     move        a,x:(r0)    b,y:(r4)
  1212.     move        a,x:(r3+n3)
  1213.     move                b,y:(r5+n5)
  1214. ;-------------------------------------------------------*
  1215. ;    Calculate span width                *
  1216. ;-------------------------------------------------------*
  1217.     move        y:ci2,b
  1218.     move        y:ci1,a
  1219.     sub        a,b        #edge_increments,r1
  1220.     rts
  1221.  
  1222. ;-------------------------------------------------------*
  1223. getflatarea:
  1224. ;-------------------------------------------------------*
  1225.     move        y:runlink_ptr,r2        ; slotlink
  1226.     move        y:rundata_ptr,r3        ; slotdata
  1227.     move        y:runsize_reg,n1
  1228.     move        y:lastslots_ptr,r5    
  1229.     move        y:HTX_ptr,r7
  1230. ;-------------------------------------------------------*
  1231.     move                y:(r0),b
  1232.     dspwaitwrite
  1233.     tfr        b,a        b,x:(r7)    ; [zone_start]
  1234.     asl        b        a,n5
  1235.     move                b,n2
  1236.     move                y:(r4),b
  1237.     sub        a,b        #0,x0
  1238.     tmi        x0,b
  1239.     dspwaitwrite
  1240.     tst        b        b,x:(r7)    ; [zone_lines]
  1241.     jeq        _vert
  1242. ;-------------------------------------------------------*
  1243.     lua        (r2)+n2,r1            ; firstlink
  1244.     lua        (r5)+n5,r5            ; lastslot
  1245.     move        y:x1list_ptr,r0
  1246.     move        y:x2list_ptr,r4
  1247.     jmp        _start_loop
  1248. ;-------------------------------------------------------*
  1249. _missing_line:
  1250. ;-------------------------------------------------------*
  1251.     dspwaitwrite
  1252.     move                b,x:(r7)
  1253.     dspwaitwrite
  1254.     move                b,x:(r7)
  1255.     jmp        _line
  1256. ;-------------------------------------------------------*
  1257. _start_loop
  1258. ;-------------------------------------------------------*
  1259.     do        b,_vert
  1260. ;-------------------------------------------------------*
  1261. _runs:    move                y:(r5)+,n2    ; ls=lastslot(y)
  1262.     clr        a        x:(r1)+n1,y1    ; fs=firstslot(y) : next firstslot-y
  1263.     clr        b        a0,x:(r2+n2)    ; runs(ls(link)) = 0
  1264.     cmp        y1,a        y1,n2        ; check for slot terminator
  1265.     jeq        _missing_line            ; repeat until slots exhausted for this (y)
  1266. ;-------------------------------------------------------*
  1267.     move                x:t,x0
  1268.     move                y:two,a
  1269.     and        #$FE,ccr
  1270.     rep        #24
  1271.     div        x0,a
  1272.     move        a0,x1
  1273.     move                y:pnorm,x0
  1274.     mpy        x0,x1,a        y:tlist_ptr,r6
  1275.     dspwaitwrite
  1276.     move                a0,x:(r7)
  1277.     move                a0,y:z
  1278. ;-------------------------------------------------------*
  1279. _slots:
  1280. ;-------------------------------------------------------*
  1281.     move        n2,n3
  1282.     move                y:x1_msk,a    ; x-mask
  1283.     move                x:(r3+n3),x0    ; x2x1 rundata
  1284.     and        x0,a        y:lshft12,x1
  1285.     mpy        x0,x1,a        a1,x0
  1286.     cmp        x0,a        a1,x1
  1287.     jle        _end_add
  1288. ;-------------------------------------------------------*
  1289. _add_run:
  1290. ;-------------------------------------------------------*
  1291.     move                y:oslist_ptr,r7
  1292.     move                x1,n0
  1293.     move                x0,n4
  1294.     move                x0,y:(r0+n0)    ; x1list(x2c) = x1c
  1295.     move                x1,y:(r4+n4)    ; x2list(x1c) = x2c
  1296.     move                y:one,a
  1297.     move                x0,n0
  1298.     move                x1,n4
  1299.     move                y:(r0+n0),y0    ; x1l = x1list(x1c)
  1300.     cmp        y0,a        y:(r4+n4),y1    ; x2r = x2list(x2c)
  1301.     jle        _left
  1302.     cmp        y1,a
  1303.     jle        _rightonly
  1304. ;-------------------------------------------------------*
  1305. _addnormal:
  1306. ;-------------------------------------------------------*
  1307.     move                x0,n7
  1308.     move                b1,n6
  1309.     add        a,b        b1,y:(r7+n7)    ; os(x1) = index
  1310.     move                x0,y:(r6+n6)    ; track(index) = x1
  1311.     jmp        _end_add
  1312. ;-------------------------------------------------------*
  1313. _left:    move                x1,n0
  1314.     move                y0,n4
  1315.     move                y0,y:(r0+n0)    ; x1list(x2c) = x1l
  1316.     move                x1,y:(r4+n4)    ; x2list(x1l) = x2c
  1317.     move                x0,n4
  1318.     move                x0,n0
  1319.     move                a0,y:(r4+n4)    ; x2list(x1c) = 0        
  1320.     move                x1,n4
  1321.     move                a0,y:(r0+n0)    ; x1list(x1c) = 0                
  1322.     move                y:(r4+n4),y1    ; x2r = x2list(x2c)
  1323.     cmp        y1,a        y0,x0        ; x1c = x1l
  1324.     jgt        _leftonly
  1325. ;-------------------------------------------------------*
  1326. _both:
  1327. ;-------------------------------------------------------*
  1328.     move                x0,n4
  1329.     move                y1,n0
  1330.     move                y1,y:(r4+n4)    ; x2list(x1c) = x2r
  1331.     move                x0,y:(r0+n0)    ; x1list(x2r) = x1c
  1332.     move                x1,n4
  1333.     move                x1,n0
  1334.     sub        a,b        a0,y:(r4+n4)    ; x2list(x2c) = 0
  1335.     move                x1,n7
  1336.     move                b1,n6
  1337.     move                y:(r7+n7),x0    ; index of right neigbour
  1338.     move                y:(r6+n6),n7    ; index of last run
  1339.     move                x0,n6
  1340.     move                a0,y:(r0+n0)    ; x1list(x2c) = 0
  1341.     move                n7,y:(r6+n6)    ; copy last index over right neighbour
  1342.     move                n6,y:(r7+n7)    ; update run to point to new index
  1343. ;-------------------------------------------------------*
  1344. _leftonly:
  1345. ;-------------------------------------------------------*
  1346.     jmp        _end_add
  1347. ;-------------------------------------------------------*
  1348. _rightonly:
  1349. ;-------------------------------------------------------*
  1350.     move                x0,n4
  1351.     move                y1,n0
  1352.     move                y1,y:(r4+n4)    ; x2list(x1c) = x2r
  1353.     move                x0,y:(r0+n0)    ; x1list(x2r) = x1c
  1354.     move                x1,n7
  1355.     move                x1,n0
  1356.     move                x1,n4
  1357.     move                a0,y:(r0+n0)    ; x1list(x2c) = 0
  1358.     move                a0,y:(r4+n4)    ; x2list(x2c) = 0
  1359.     move                y:(r7+n7),n6    ; index of right neighbour
  1360.     move                x0,n7
  1361.     move                x0,y:(r6+n6)    ; point index to new run
  1362.     move                n6,y:(r7+n7)
  1363. ;-------------------------------------------------------*
  1364. _end_add:
  1365. ;-------------------------------------------------------*
  1366.     clr        a        x:(r2+n2),y1    ; locate next slot for this (y)
  1367.     cmp        y1,a        y1,n2
  1368.     jne        _slots
  1369. ;-------------------------------------------------------*
  1370. _getruns:
  1371. ;-------------------------------------------------------*
  1372.     move                y:HTX_ptr,r7
  1373.     dspwaitwrite
  1374.     tst        b        b,x:(r7)
  1375.     jeq        _fetch
  1376. ;-------------------------------------------------------*
  1377. ;    Send luminance back to host            *
  1378. ;-------------------------------------------------------*
  1379.     move                y:z,a
  1380.     neg        a        y:lshft19,x1
  1381.     add        x1,a        y:lshft5,x1
  1382.     move        a1,x0
  1383.     mpy        x0,x1,a
  1384.     move        a0,x0
  1385.     mpy        x0,x0,a
  1386.     move        a1,x0
  1387.     mpy        x0,x0,a        y:rshft5,x1
  1388.     move        a1,x0
  1389.     mpy        x0,x1,a        y:mlum2,x1
  1390.     move        a1,x0
  1391.     mpy        x0,x1,a        y:mlum3,x0
  1392.     add        x0,a        #0,x1
  1393.     tmi        x1,a
  1394.     move                y:mlum_limit,x1
  1395.     cmp        x1,a
  1396.     tpl        x1,a
  1397. ;-------------------------------------------------------*
  1398.     dspwaitwrite
  1399.     clr        a        a,x:(r7)    
  1400. ;-------------------------------------------------------*
  1401. ;    Send joined runs back to host            *
  1402. ;-------------------------------------------------------*
  1403.     do        b1,_fetch
  1404.     move                y:(r6)+,n4    ; n4 = x1
  1405.     dspwaitwrite
  1406.     move                n4,x:(r7)
  1407.     move                y:(r4+n4),n0    ; n0 = x2 
  1408.     move                a0,y:(r4+n4)
  1409.     dspwaitwrite
  1410.     move                n0,x:(r7)
  1411.     move                a0,y:(r0+n0)
  1412. ;-------------------------------------------------------*
  1413. _fetch:    nop
  1414. ;-------------------------------------------------------*
  1415. _line:    move                l:ti,a
  1416.     move                l:t,b
  1417.     add        a,b
  1418.     clr        a        b,l:t
  1419. ;-------------------------------------------------------*
  1420. _vert:    rts
  1421.  
  1422. ;-------------------------------------------------------*
  1423. GetSsector:
  1424. ;-------------------------------------------------------*
  1425.     move        #<$FF,b
  1426.     move        y:HTX_ptr,r7
  1427.     dspread_s
  1428.     move        a,y:cy
  1429.     dspread_s
  1430.     move        a,y:fy
  1431. ;-------------------------------------------------------*
  1432.     move                y:lshft8,x0
  1433.     move                y:hhig,y0
  1434.     mpy        x0,y0,a        y:vs_width,y1
  1435.     move                y:cy,x1
  1436.     mpy        x0,y1,a        a0,y0
  1437.     move                a0,y1
  1438.     mpy        x1,y1,a        y:fy,x1
  1439.     rep        #(6+2)
  1440.     asr        a        a0,b
  1441.     add        y0,b        a0,a        ; ((w/2) * cy) / cz2 (128)
  1442.     add        y0,a        b,y:cj1        ; cj1
  1443.     mpy        x1,y1,a        a,y:cj2        ; cj2
  1444.     rep        #(6+2)
  1445.     asr        a        a0,b
  1446.     add        y0,b        a0,a        ; ((w/2) * fy) / fz2 (128)
  1447.     add        y0,a        b,y:fj1        ; fj1
  1448.     move                a,y:fj2        ; fj2
  1449. ;-------------------------------------------------------*
  1450.     move                y:lshft8,x0
  1451. ;-------------------------------------------------------*
  1452.     move                y:cj2,b
  1453.     move                y:cj1,x1
  1454.     sub        x1,b        y:ctop,y0        ; (cj2-cj1)
  1455.     mpy        x0,y0,a        #>(1<<8)-1,y0
  1456.     move        a0,a                    ; cj3
  1457.     add        y0,a        y:cj1,y0
  1458.     sub        y0,a        y:lshft15,y0
  1459.     move        a,y1                    ; (cj3-cj1)
  1460.     mpy        -y0,y1,a    b,x1            ; (cz1-cz2)*(cj3-cj1)
  1461.     jsr        divs_x1_a
  1462.     move                #>(128<<8),y1
  1463.     add        y1,a        y:cbot,y0
  1464.     move                a,y:ctop_z
  1465.     mpy        x0,y0,a        #>(1<<8)-1,y0
  1466.     move        a0,a                    ; cj3
  1467.     add        y0,a        y:cj1,y0
  1468.     sub        y0,a        y:lshft15,y0
  1469.     move        a,y1
  1470.     mpy        -y0,y1,a    #>(128<<8),y1
  1471.     jsr        divs_x1_a
  1472.     add        y1,a
  1473.     move                a,y:cbot_z
  1474. ;-------------------------------------------------------*
  1475.     move                y:fj1,b
  1476.     move                y:fj2,x1
  1477.     sub        x1,b        y:ftop,y0        ; (cj2-cj1)
  1478.     mpy        x0,y0,a        #>(1<<8)-1,y0
  1479.     move        a0,a                    ; cj3
  1480.     add        y0,a        y:fj1,y0
  1481.     sub        y0,a        y:lshft15,y0
  1482.     move        a,y1                    ; (cj3-cj1)
  1483.     mpy        y0,y1,a        b,x1            ; (cz1-cz2)*(cj3-cj1)<<8
  1484.     jsr        divs_x1_a
  1485.     move                #>(128<<8),y1
  1486.     add        y1,a        y:fbot,y0
  1487.     move                a,y:ftop_z
  1488.     mpy        x0,y0,a        #>(1<<8)-1,y0
  1489.     move        a0,a                    ; cj3
  1490.     add        y0,a        y:fj1,y0
  1491.     sub        y0,a        y:lshft15,y0
  1492.     move        a,y1
  1493.     mpy        y0,y1,a        #>(128<<8),y1
  1494.     jsr        divs_x1_a
  1495.     add        y1,a
  1496.     move                a,y:fbot_z
  1497. ;-------------------------------------------------------*
  1498. ;    Initialise gradient & send span    to host        *
  1499. ;-------------------------------------------------------*
  1500.     move                y:ctop,a
  1501.     move                y:cbot,b
  1502.     sub        a,b        y:one,a
  1503.     move        b,x0
  1504.     and        #$FE,ccr
  1505.     rep        #24
  1506.     div        x0,a
  1507.     move        a0,x0
  1508.     move                y:ctop_z,a
  1509.     move                y:cbot_z,b
  1510.     sub        a,b        a,l:t
  1511.     move        b,x1
  1512.     mpy        x0,x1,a        #ctop,r0
  1513.     move                a,l:ti
  1514.     move                #cbot,r4
  1515.     jsr        getflatarea
  1516. ;-------------------------------------------------------*
  1517. ;    Initialise gradient & send span    to host        *
  1518. ;-------------------------------------------------------*
  1519.     move                y:ftop,a
  1520.     move                y:fbot,b
  1521.     sub        a,b        y:one,a
  1522.     move        b,x0
  1523.     and        #$FE,ccr
  1524.     rep        #24
  1525.     div        x0,a
  1526.     move        a0,x0
  1527.     move                y:ftop_z,a
  1528.     move                y:fbot_z,b
  1529.     sub        a,b        a,l:t
  1530.     move        b,x1
  1531.     mpy        x0,x1,a        #ftop,r0
  1532.     move                a,l:ti
  1533.     move                #fbot,r4
  1534.     jsr        getflatarea
  1535. ;-------------------------------------------------------*
  1536.     jmp        command_base
  1537.  
  1538. ;-------------------------------------------------------*
  1539. divs_x1_a:
  1540. ;-------------------------------------------------------*
  1541.     abs        a        a,y:0
  1542.     and        #$FE,ccr
  1543.     rep        #24
  1544.     div        x1,a
  1545.     move        a0,a
  1546.     jclr        #23,y:0,_nn
  1547.     neg        a
  1548. _nn:    rts
  1549.  
  1550. ;-------------------------------------------------------*
  1551. RotateMap:
  1552. ;-------------------------------------------------------*
  1553. ;    x1                    ; 24
  1554. ;    y1                    ; 24
  1555. ;    v                    ; 16.8
  1556. ;-------------------------------------------------------*
  1557. ;    tnorm = 320/h                ; 8.16
  1558. ;    xo = (w/2)-x1                ; 24
  1559. ;    uvs = v * tnorm                ; 16.8
  1560. ;    xi = uvs * cos(a)            ; 16.24
  1561. ;    yi = uvs * sin(a)            ; 16.24
  1562. ;    xs = uvs*xo*cos(a) + v*sin(a) - py    ; 16.24
  1563. ;    ys = uvs*xo*sin(a) - v*cos(a) - px    ; 16.24
  1564. ;-------------------------------------------------------*
  1565.     move        #HTX,r0
  1566.     move        #map_data,r7
  1567.     move        #4-1,m7
  1568.     move        #_next,r5
  1569. ;-------------------------------------------------------*
  1570. ;    Read untranslated coordinates from host        *    
  1571. ;-------------------------------------------------------*
  1572. _next:    dspwaitread
  1573.     move                x:(r0),a
  1574.     jset        #11,a,_stop_loop
  1575.     dspwaitread
  1576.     move        x:(r0),x1    y:(r7)+,b
  1577. ;-------------------------------------------------------*
  1578. ;    Write previously derived coordinates to host    *
  1579. ;-------------------------------------------------------*
  1580.     dspwaitwrite
  1581.     move        b,x:(r0)    y:(r7)+,b
  1582.     dspwaitwrite
  1583.     move        b,x:(r0)    y:(r7)+,b
  1584.     dspwaitwrite
  1585.     move        b,x:(r0)    y:(r7)+,b
  1586.     dspwaitwrite
  1587.     move        b,x:(r0)
  1588. ;-------------------------------------------------------*
  1589. ;    xo(24) = (w/2)-x1                *
  1590. ;-------------------------------------------------------*
  1591.     move        a,n0
  1592.     move        n0,a
  1593.     move                y:hwid,x0    ; ???
  1594.     sub        x0,a        x1,y:map_v
  1595.     move                a,y:map_xo    
  1596. ;-------------------------------------------------------*
  1597. ;    uvs(16.8) = v * tnorm                *
  1598. ;-------------------------------------------------------*
  1599.     move                y:map_tnorm,x0
  1600.     move                y:map_v,x1
  1601.     mpy        x0,x1,a        y:cosa,x1
  1602.     rep        #(8-1)
  1603.     asl        a
  1604.     move        a,x0
  1605. ;-------------------------------------------------------*
  1606. ;    xi(24.24) = uvs * cos(a)            *
  1607. ;-------------------------------------------------------*
  1608.     mpy        x0,x1,a        x0,y:map_uvs    
  1609.     asl        a        #>1<<(23-(8-2)),x1
  1610.     move                a,y:map_cosauvs
  1611.     move        a,y0
  1612.     mpy        y0,x1,a        y:sina,x1
  1613. ;-------------------------------------------------------*
  1614. ;    yi(24.24) = uvs * sin(a)            *
  1615. ;-------------------------------------------------------*
  1616.     mpy        x0,x1,a        a,y:map_xi
  1617.     asl        a        #>1<<(23-(8-2)),x1
  1618.     move                a,y:map_sinauvs
  1619.     move        a,x0
  1620.     mpy        -x0,x1,a    y:map_cosauvs,x0
  1621. ;-------------------------------------------------------*
  1622. ;    xs = uvs*xo*cos(a) + v*sin(a) - py        *
  1623. ;-------------------------------------------------------*
  1624.     move                y:map_xo,x1
  1625.     mpy        x0,x1,a        a,y:map_yi
  1626.     rep        #8-1
  1627.     asl        a                ; 8.24
  1628.     move                y:map_v,x1
  1629.     move                y:sina,x0
  1630.     mpy        x0,x1,b        #>1<<(23-8),x1
  1631.     asl        b
  1632.     move        b,x0
  1633.     mpy        x0,x1,b        y:py,x0
  1634.     add        b,a        y:map_xo,y1
  1635.     sub        x0,a        y:map_sinauvs,x0
  1636.     rep        #8+2
  1637.     asl        a
  1638. ;-------------------------------------------------------*
  1639. ;    ys = uvs*xo*sin(a) - v*cos(a) - px        *
  1640. ;-------------------------------------------------------*
  1641.     mpy        -x0,y1,a    a,y:map_x
  1642.     rep        #8-1
  1643.     asl        a                ; 8.24
  1644.     move                y:map_v,x1
  1645.     move                y:cosa,x0
  1646.     mpy        x0,x1,b        #>1<<(23-8),x1
  1647.     asl        b
  1648.     move        b,x0
  1649.     mac        x0,x1,a        y:px,x0
  1650.     add        x0,a
  1651.     rep        #8+2
  1652.     asl        a
  1653.     move                a,y:map_y
  1654.     jmp        (r5)
  1655. ;-------------------------------------------------------*
  1656. _stop_loop:
  1657. ;-------------------------------------------------------*
  1658.     move        #-1,m7
  1659.     jmp        command_base
  1660.  
  1661. ;-------------------------------------------------------*
  1662. NewSsector:
  1663. ;-------------------------------------------------------*
  1664.     move        y:runlink_ptr,r2
  1665.     move        y:runsize_reg,n2
  1666.     move        y:height,x1
  1667.     tfr        x1,a        #0,x0
  1668.     asl        a        y:lastslots_ptr,r6
  1669.     move                a,y:index
  1670.     clr        b        n2,a1
  1671.     rep        x1
  1672.     add        a,b    x0,x:(r2)+n2    b,y:(r6)+
  1673. _vert:    move        #0,b1
  1674.     move        b1,y:cbot
  1675.     move        b1,y:fbot
  1676.     move        #256,b1
  1677.     move        b1,y:ctop
  1678.     move        b1,y:ftop
  1679.     jmp        command_base
  1680.  
  1681. ;-------------------------------------------------------*
  1682. ScanOcclusion:
  1683. ;-------------------------------------------------------*
  1684.     clr        a    y:HTX_ptr,r7
  1685.     dspread_u    x0
  1686.     dspread_u    b
  1687.     sub        x0,b    #occlusion_list,a1
  1688.     add        x0,a    #visible_code,n1
  1689.     move        a,r0
  1690.     move        #1,a1
  1691. _loop:    jset        #0,y:(r0)+,_out
  1692.     sub        a,b
  1693.     jne        _loop
  1694. _err:    move        #invisible_code,n1
  1695. _out:    dspwaitwrite
  1696.     move        n1,x:(r7)
  1697.     jmp        command_base
  1698.  
  1699. ;-------------------------------------------------------*
  1700. NodeInCone:
  1701. ;-------------------------------------------------------*
  1702.     move        #noderegs,r0
  1703.     move        #<$FF,b
  1704.     move        y:HTX_ptr,r7
  1705.     move        y:py,y0
  1706.     do        #2,_ry
  1707.     dspread_s
  1708.     sub        y0,a
  1709.     move        a,y:(r0)+
  1710. _ry:    move        y:px,x0
  1711.     do        #2,_rx
  1712.     dspread_s
  1713.     sub        x0,a
  1714.     move        a,y:(r0)+
  1715. _rx:
  1716.  
  1717. ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*
  1718. ;    Octal segment BSP node eliminator        *
  1719. ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*
  1720. ;    Eliminates nodes from 5 out of all 8 octants    *
  1721. ;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*
  1722. octant_check:
  1723. ;-------------------------------------------------------*
  1724.     move        #project_node,r0
  1725.     move        #oct_x,r1
  1726.     move                y:pangle,x1
  1727.     move                y:rshft13,y1
  1728.     mpy        x1,y1,a        y:octs_ptr,r7
  1729.     move        a1,n7
  1730.     nop
  1731.     move                y:(r7+n7),r7
  1732.     nop
  1733.     jmp        (r7)
  1734. ;-------------------------------------------------------*
  1735. ;    Octant elimination routines             *
  1736. ;-------------------------------------------------------*
  1737. oct_0:    move                y:node_y1,a
  1738.     tst        a        y:node_x2,b
  1739.     jpl        (r1)
  1740.     cmp        a,b
  1741.     jpl        (r0)
  1742.     jmp        (r1)
  1743. ;-------------------------------------------------------*
  1744. oct_1:    move                y:node_x2,a
  1745.     tst        a        y:node_y1,b
  1746.     jmi        (r1)
  1747.     cmp        b,a
  1748.     jpl        (r0)
  1749.     jmp        (r1)
  1750. ;-------------------------------------------------------*
  1751. oct_2:    move                y:node_x2,a
  1752.     tst        a        y:node_y2,b
  1753.     jmi        (r1)
  1754.     neg        b
  1755.     cmp        b,a
  1756.     jpl        (r0)
  1757.     jmp        (r1)
  1758. ;-------------------------------------------------------*
  1759. oct_3:    move                y:node_y2,a
  1760.     tst        a        y:node_x2,b
  1761.     jmi        (r1)
  1762.     neg        a
  1763.     cmp        a,b
  1764.     jpl        (r0)
  1765.     jmp        (r1)
  1766. ;-------------------------------------------------------*
  1767. oct_4:    move                y:node_y2,a
  1768.     tst        a        y:node_x1,b
  1769.     jmi        (r1)
  1770.     cmp        a,b
  1771.     jmi        (r0)
  1772.     jmp        (r1)
  1773. ;-------------------------------------------------------*
  1774. oct_5:    move                y:node_x1,a
  1775.     tst        a        y:node_y2,b
  1776.     jpl        (r1)
  1777.     cmp        b,a
  1778.     jmi        (r0)
  1779.     jmp        (r1)
  1780. ;-------------------------------------------------------*
  1781. oct_6:    move                y:node_x1,a
  1782.     tst        a        y:node_y1,b
  1783.     jpl        (r1)
  1784.     neg        b
  1785.     cmp        b,a
  1786.     jmi        (r0)
  1787.     jmp        (r1)
  1788. ;-------------------------------------------------------*
  1789. oct_7:    move                y:node_y1,a
  1790.     tst        a        y:node_x1,b
  1791.     jpl        (r1)
  1792.     neg        a
  1793.     cmp        a,b
  1794.     jmi        (r0)
  1795. ;-------------------------------------------------------*
  1796. oct_x:    move        #invisible_code,n0
  1797.     dspwaitwrite
  1798.     move        n0,x:HTX
  1799.     jmp        command_base
  1800.  
  1801. ;-------------------------------------------------------*
  1802. project_node:
  1803. ;-------------------------------------------------------*
  1804.     move        #>$1000,a
  1805.     neg        a        a,y:node_imin
  1806.     move                a,y:node_imax
  1807. ;-------------------------------------------------------*
  1808. ;    Rotate NY & NZ into canonical VZ & VX        *
  1809. ;-------------------------------------------------------*
  1810. ;    x = (dx)*sin(a) + (dy)*cos(a)            *
  1811. ;    z = (dx)*cos(a) - (dy)*sin(a)            *
  1812. ;-------------------------------------------------------*
  1813.     move                y:sina,x1
  1814.     move                y:cosa,y1
  1815.     move                y:node_x1,x0
  1816.     mpy        x0,x1,a        y:node_y1,y0    ; dx*sin(a)
  1817.     mac        y0,y1,a        #vertices,r0    ; + dy*cos(a)
  1818.     mpy        x0,y1,b        #1,n3        ; dx*cos(a)
  1819.     mac        -y0,x1,b    y:node_x2,x0    ; - dy*sin(a)
  1820.     asl        a        y:node_y1,y0
  1821.     asl        b        a,l:(r0)+
  1822.     mpy        x0,x1,a        b,l:(r0)+    ; dx*sin(a)
  1823.     mac        y0,y1,a        #1,n1        ; + dy*cos(a)
  1824.     mpy        x0,y1,b        #1,n2        ; dx*cos(a)
  1825.     mac        -y0,x1,b    y:node_x2,x0    ; - dy*sin(a)
  1826.     asl        a        y:node_y2,y0
  1827.     asl        b        a,l:(r0)+
  1828.     mpy        x0,x1,a        b,l:(r0)+    ; dx*sin(a)
  1829.     mac        y0,y1,a                ; + dy*cos(a)
  1830.     mpy        x0,y1,b                ; dx*cos(a)
  1831.     mac        -y0,x1,b    y:node_x1,x0    ; - dy*sin(a)
  1832.     asl        a        y:node_y2,y0
  1833.     asl        b        a,l:(r0)+
  1834.     mpy        x0,x1,a        b,l:(r0)+    ; dx*sin(a)
  1835.     mac        y0,y1,a                ; + dy*cos(a)
  1836.     mpy        x0,y1,b                ; dx*cos(a)
  1837.     mac        -y0,x1,b            ; - dy*sin(a)
  1838.     asl        a        
  1839.     asl        b        a,l:(r0)+
  1840.     move                b,l:(r0)+    ; dx*sin(a)
  1841. ;-------------------------------------------------------*
  1842. project_line:
  1843. ;-------------------------------------------------------*
  1844.     move        #_swap,r4
  1845.     move        #_abort_line,r5
  1846.     move        #_no_isect,r6
  1847.     move        #linedefs,r7
  1848. ;-------------------------------------------------------*
  1849.     do        #4,_project_loop
  1850. ;-------------------------------------------------------*
  1851.     move        y:(r7)+,r1
  1852.     move        y:(r7),r2
  1853. ;-------------------------------------------------------*
  1854. ;    Reorient line                    *
  1855. ;-------------------------------------------------------*
  1856.     move                l:(r1+n1),a
  1857.     move                l:(r2+n2),b
  1858.     cmp        a,b        r1,n0
  1859.     jpl        (r4)
  1860.     move        r2,r1
  1861.     move        n0,r2
  1862. ;-------------------------------------------------------*
  1863. ;    z-clip line if it intersects with viewplane    *    
  1864. ;-------------------------------------------------------*
  1865. _swap:    move                y:zmin,a
  1866.     move                l:(r2+n2),b
  1867.     cmp        b,a        l:(r1+n1),x
  1868.     jpl        (r5)
  1869. ;-------------------------------------------------------*
  1870.     move                r1,r3
  1871.     sub        x,a        l:(r1),x    ; (cz-z1)
  1872.     jle        (r6)
  1873. ;-------------------------------------------------------*
  1874.     move                #temp1,r3
  1875.     move                l:(r2),b
  1876.     sub        x,b        a1,x0        ; (cz-z1)
  1877.     move                b1,y0        ; (x2-x1)
  1878.     mpy        x0,y0,a        l:(r2+n2),b    ; (x2-x1)*(cz-z1)
  1879.     move                l:(r1+n1),y
  1880.     sub        y,b        a,y:0
  1881.     abs        a        b1,x0        ; (z2-z1)
  1882.     and        #$FE,ccr
  1883.     rep        #24
  1884.     div        x0,a                ; ((x2-x1)*(cz-z1))/(z2-z1)
  1885.     move        a0,a
  1886.     jclr        #0,y:0,_nneg
  1887.     neg        a
  1888. ;-------------------------------------------------------*
  1889. _nneg:    move                l:(r1),b
  1890.     add        a,b        y:zmin,a
  1891.     move        b,x:(r3)            ; x1' = (((x2-x1)*(cz-z1))/(z2-z1))+x1
  1892.     move        a,x:(r3+n3)            ; z1' = cz
  1893. ;-------------------------------------------------------*
  1894. _no_isect:
  1895. ;-------------------------------------------------------*
  1896. ;    Project points into 2D viewspace        *
  1897. ;-------------------------------------------------------*
  1898.     move                y:hs_width,x0
  1899.     move                x:(r3),x1    ; x1
  1900.     move                x:(r2),y1    ; x2
  1901.     mpy        x0,x1,a        x:(r3+n3),x1    ; z1
  1902.     mpy        x0,y1,b        x:(r2+n2),y1    ; z2
  1903.     move                a0,x0        ; (x1*hw)*2
  1904.     move                y:one,a
  1905.     tfr        a,b        b0,y0        ; (x2*hw)*2
  1906.     and        #$FE,ccr
  1907.     rep        #24
  1908.     div        x1,a
  1909.     move                a0,x1        ; 1/(z1*2)
  1910.     and        #$FE,ccr
  1911.     rep        #24
  1912.     div        y1,b
  1913.     mpy        -x0,x1,a    b0,y1        ; 1/(z2*2)
  1914.     asr        a
  1915.     mpy        -y0,y1,b    a1,x0        ; (x1*hw) / z1 
  1916.     asr        b        y:node_imin,a
  1917.     move                b1,y0        ; (x2*hw) / z2
  1918. ;-------------------------------------------------------*
  1919. ;    Adjust bounds                    *
  1920. ;-------------------------------------------------------*
  1921.     cmp        x0,a        y:node_imax,b
  1922.     tgt        x0,a
  1923.     cmp        y0,a
  1924.     tgt        y0,a
  1925.     cmp        x0,b        a,y:node_imin
  1926.     tmi        x0,b
  1927.     cmp        y0,b
  1928.     tmi        y0,b
  1929.     move                b,y:node_imax
  1930. ;-------------------------------------------------------*
  1931. _abort_line:
  1932. ;-------------------------------------------------------*
  1933.     move        #0,y0
  1934. ;-------------------------------------------------------*
  1935. _project_loop:
  1936. ;-------------------------------------------------------*
  1937. ;    Inside / outside viewport check            *
  1938. ;-------------------------------------------------------*
  1939.     move                y:one,y1
  1940.     move                y:hwid,x0
  1941.     move                y:node_imax,b
  1942.     add        y1,b        #_end,r1
  1943.     add        x0,b        y:node_imin,a
  1944.     jmi        (r1)
  1945.     sub        y1,a        #_loop,r3
  1946.     add        x0,a        y:width,x1
  1947.     tmi        y0,a
  1948.     cmp        x1,a        #occlusion_list,r2
  1949.     jpl        (r1)
  1950.     cmp        x1,b        a,n2
  1951.     tpl        x1,b
  1952.     sub        a,b        (r2)+n2
  1953.     jle        (r1)
  1954. ;-------------------------------------------------------*
  1955. ;    Occlusion check                    *
  1956. ;-------------------------------------------------------*
  1957.     move        y:one,a
  1958. _loop:    jset        #0,y:(r2)+,_out
  1959.     sub        a,b
  1960.     jne        (r3)
  1961. ;-------------------------------------------------------*
  1962. _end:    move        #invisible_code,n1
  1963. _out:    dspwaitwrite
  1964.     move        n1,x:HTX
  1965.     jmp        command_base
  1966.  
  1967.  
  1968.  
  1969. ;-------------------------------------------------------*
  1970. NewScene:
  1971. ;-------------------------------------------------------*
  1972.     move        #<$FF,b
  1973.     move        y:HTX_ptr,r7
  1974.     dspread_s    
  1975.     move        a,y:px
  1976.     dspread_s    
  1977.     move        a,y:py
  1978.     dspread_u    a
  1979.     move        a,y:pangle
  1980.     dspread        y:sina
  1981.     dspread        y:cosa
  1982.     move        #occlusion_list,r7
  1983.     move        #(miny+1),r0
  1984.     move        #(maxy+1),r4
  1985.     move        #1,a0
  1986.     move        #0,y0
  1987.     move        y:height,y1
  1988.     do        y:width,_clr
  1989.     move        a0,y:(r7)+
  1990.     move        y0,x:(r0)+
  1991.     move        y1,y:(r4)+
  1992. _clr:    jmp        command_base
  1993.  
  1994. ;-------------------------------------------------------*
  1995. ResizeWindow:
  1996. ;-------------------------------------------------------*
  1997.     move        y:HTX_ptr,r7
  1998.     move        #window_regs,r0
  1999.     do        #2,_lp1
  2000.     dspread_u    a
  2001.     asr        a        a,y:(r0)+
  2002.     move                a,y:(r0)+
  2003. _lp1:    do        #3,_lp2
  2004.     dspread_u    a
  2005.     move                a,y:(r0)+
  2006. _lp2:
  2007. ;-------------------------------------------------------*
  2008.     move        y:x1list_ptr,r0
  2009.     move        y:x2list_ptr,r4
  2010.     clr        b        y:width,a
  2011.     move        #2,b1
  2012.     add        b,a
  2013.     do        a1,_clr
  2014.     move        a0,y:(r0)+
  2015.     move        a0,y:(r4)+
  2016. ;-------------------------------------------------------*
  2017. ;    tnorm(8.16) = ((320/hwid)*vs_width)/r_height    *
  2018. ;-------------------------------------------------------*
  2019. _clr:    move        y:r_height,x0
  2020.     move        #>320,a
  2021.     rep        #(8-1)
  2022.     asr        a
  2023.     and        #$FE,ccr
  2024.     rep        #24
  2025.     div        x0,a
  2026.     move        a0,x0
  2027.     move                y:vs_width,x1
  2028.     mpy        x0,x1,a        y:hwid,x1
  2029.     and        #$FE,ccr
  2030.     rep        #24
  2031.     div        x1,a
  2032.     move        a0,y:map_tnorm
  2033.     jmp        command_base
  2034.  
  2035. ;-------------------------------------------------------*
  2036. LinearCalc:
  2037. ;-------------------------------------------------------*
  2038.     move        #linear_column,r0
  2039.     move        r0,y:column_rout
  2040.     jmp        command_base
  2041.  
  2042. ;-------------------------------------------------------*
  2043. PerspectCalc:
  2044. ;-------------------------------------------------------*
  2045.     move        #perspected_column,r0
  2046.     move        r0,y:column_rout
  2047.     jmp        command_base
  2048.  
  2049. ;-------------------------------------------------------*
  2050. SetLuminance:
  2051. ;-------------------------------------------------------*
  2052.     dspread        n0
  2053.     move        n0,y:luminance
  2054.     move        n0,x0
  2055.  
  2056.     move        #>255,a
  2057.     sub        x0,a        ; ldiff
  2058.     move        a,x1
  2059.  
  2060.     mpy        x0,x1,b
  2061.     rep        #3+2
  2062.     asr        b
  2063.     move        b0,y:mlum2
  2064.  
  2065.     asr        a
  2066.     asr        a
  2067.     neg        a
  2068.     add        x0,a
  2069.     asr        a
  2070.     asr        a
  2071.     move        a,y:mlum3
  2072.             
  2073.     jmp        command_base
  2074.     
  2075. ;-------------------------------------------------------*
  2076. start:
  2077. ;-------------------------------------------------------*
  2078.     bclr        #2,omr
  2079. ;-------------------------------------------------------*
  2080. command_base:
  2081. ;-------------------------------------------------------*
  2082. ;    Execute next command                *
  2083. ;-------------------------------------------------------*
  2084.     clr        b    y:commands_ptr,r7
  2085.     dspwaitread    
  2086.     move        x:HTX,a2
  2087.     move        a2,n7
  2088.     nop
  2089.     move        y:(r7+n7),r7
  2090.     nop
  2091.     jmp        (r7)
  2092.  
  2093. ;-------------------------------------------------------*
  2094. ;    Mark end of external program space        *
  2095. ;-------------------------------------------------------*
  2096.  
  2097. endcode:
  2098. lasty        =    endcode
  2099.  
  2100. ;-------------------------------------------------------*
  2101.     org        y:lasty
  2102. ;-------------------------------------------------------*
  2103. ;    External Y-buffers                *
  2104. ;-------------------------------------------------------*
  2105.  
  2106. occlusion_list:        ds    320
  2107.  
  2108. lastslots:        ds    256
  2109.  
  2110. x1list:            ds    320+2
  2111. x2list:            ds    320+2
  2112. oslist:            ds    320+2
  2113. tlist:            ds    16
  2114.  
  2115. ;-------------------------------------------------------*
  2116.